package com.example.blog.contorller;

import com.example.blog.common.page.Page;
import com.example.blog.po.Article;
import com.example.blog.service.IArticleService;
import com.github.pagehelper.PageInfo;
import io.lettuce.core.ScriptOutputType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.NumberUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class ArticleController {
    @Autowired
    private IArticleService articleService;

    @Value("${defaultPageSize}")
    private int defaultPageSize;

    @RequestMapping("/getArticles")
    public String getArticles(Model model, Integer pageNum, Integer pageSize){
        if(pageNum == null || pageNum == 0){
            pageNum = 1;
        }

        if(pageSize == null || pageNum == 0){
            pageSize = defaultPageSize;
        }

        PageInfo<Article> pagedArticles = articleService.getPagedArticles(pageNum, pageSize);

        model.addAttribute("pagedData", pagedArticles);

        System.out.println("pageNum=" + pageNum  + ",pageSize=" + pageSize );

        return "client/index";
    }

}
